<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one community member based on id
			get_school();
			break;
		case 2:
		     get_all_schools();
			break;
		case 3:
            update_school();
            break;
        case 4:
            add_school();
            break;
        case 5:
		     delete_school();
			 break;
		case 6:
            search_school();
             break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_school(){
		include_once("schools.php");
		
		$id=get_datan("id");
		$v=new schools();
		$row=$v->get_school($id);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","schools not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"school":{';
			echo jsonn("id",$id).",";
			
			echo jsons("school_id",$row['school_id']).",";
			//name
			echo jsons("school_name",$row['school_name']).",";
			//school notes
			echo jsons("sch_notes",$row['sch_notes']).",";
			//community_id
			echo jsonn("community_id",$row['community_id']);
			
			
			echo "}";
		echo "}";
	}

function add_school()
{

    $school_name = get_data('sna');;
    $sch_notes = get_data('snn');
    $community_id = get_datan('cd');


    include("schools.php");
    $v = new schools();
    if(!$v-> add_school($school_name,$sch_notes,$community_id))
    {
        echo '{"result": 0,"message":"adding failed"}';
    }
    else
    {
        echo '{"result": 1,"message":"adding successful"}';
    }
}
	
	function update_school()
    {
	include_once("schools.php");
	       $school_id=get_datan('sid');

		   $school_name=get_data('sn');

		   $sch_notes=get_data('schn');
			
		   $community_id=get_datan('ci');
		  
		   $v = new schools();
		   $row = $v->update_school($school_id,$school_name,$sch_notes,$community_id);
		   if(!$row)
           {
		      //return error message
			  echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not correct");
			echo "}";
			return;
			  
		   }

			 // http://localhost/webtech/project2/vaccine_action.php?cmd=2&vid=100&vn=newname&vs=7&vu=www.google.com
	 }

    function delete_school()
    {
        $school_id=get_datan('sid');
        include_once("schools.php");
        $v = new schools();

        if(!$v->delete_school($school_id))
        {
            //return error message
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","id not correct");
            echo "}";
            return;

        }
    }


?>